package org.edu.modules.classSchedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.classChoise.entity.JwKechengClass;
import org.edu.modules.classChoise.mapper.JwKechengClassMapper;
import org.edu.modules.classSchedule.entity.*;
import org.edu.modules.classSchedule.mapper.*;
import org.edu.modules.classSchedule.service.IVeJwPaikeService;
import org.edu.modules.classSchedule.vo.CourseArrange;
import org.edu.modules.classSchedule.vo.ScheduleInfo;
import org.edu.modules.jxrw.entity.VeJwJxrw;
import org.edu.modules.jxrw.mapper.VeJwJxrwMapper;
import org.edu.modules.pub.entity.VeJwJiaoshi;
import org.edu.modules.pub.mapper.VeJwJiaoshiMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 排课信息
 * @Author:
 * @Date:   2021-05-13
 * @Version: V1.0
 */
@Service
public class VeJwPaikeServiceImpl extends ServiceImpl<VeJwPaikeMapper, VeJwPaike> implements IVeJwPaikeService {
    @Autowired
    private VeJwPaikeMapper veJwPaikeMapper;
    @Autowired
    private VeJwPkmxClassMapper veJwPkmxMapper;
    @Autowired
    private VeJwJxrwMapper veJwJxrwMapper;
    @Autowired
    private VeJwPkpzMapper veJwPkpzMapper;
    @Autowired
    private VeJwSectionMapper veJwSectionMapper;
    @Autowired
    private VeJwBpksjMapper veJwBpksjMapper;
    @Autowired
    private JwKechengClassMapper jwKechengMapper;
    @Autowired
    private VeJwJiaoshiMapper veJwJiaoshiMapper;


    private ScheduleInfo scheduleInfo;//入参
    private VeJwPkpz veJwPkpz;        //配置信息
    private List<CourseArrange> caList;//所有课程时间
    private Map<Integer,String> keChengMap;  //课程基础信息
    private Map<Integer,String> jiaoShiMap;//
    private int pkmxCount;
    private String jxbmcShow;

    @Override
    public String checkNew(VeJwPaike veJwPaike) {
        return "";
    }

    @Override
    public String autoSchedule(ScheduleInfo scheduleInfo) {
        this.scheduleInfo = scheduleInfo;
        this.veJwPkpz = veJwPkpzMapper.selectById(1);
        this.caList = initCourseArrange();
        JwKechengClass jwKecheng = new JwKechengClass();
        jwKecheng.setIsdelete(0);
        QueryWrapper<JwKechengClass> queryWrapper = QueryGenerator.initQueryWrapper(jwKecheng, null);
        List<JwKechengClass> keChengList = jwKechengMapper.selectList(queryWrapper);
        keChengMap = new HashMap<>();
        for(JwKechengClass kc:keChengList){
            keChengMap.put(kc.getId(),kc.getKcmc());
        }
        VeJwJiaoshi veJwJiaoshi = new  VeJwJiaoshi();
        veJwJiaoshi.setIsdelete(0);
        QueryWrapper<VeJwJiaoshi> queryWrapperJs = QueryGenerator.initQueryWrapper(veJwJiaoshi, null);
        List<VeJwJiaoshi> jiaoshiList = veJwJiaoshiMapper.selectList(queryWrapperJs);
        jiaoShiMap = new HashMap<>();
        for(VeJwJiaoshi js:jiaoshiList){
            jiaoShiMap.put(js.getId(),js.getJsmc());
        }

        pkmxCount = 0;
        jxbmcShow = "";
        VeJwJxrw veJwJxrw = new VeJwJxrw();
        veJwJxrw.setSemid(scheduleInfo.getSemId());
        veJwJxrw.setCheckstatus(1);
        QueryWrapper<VeJwJxrw> queryWrapper2 = QueryGenerator.initQueryWrapper(veJwJxrw, null);
        queryWrapper2.orderByAsc("jxbid");
        List<VeJwJxrw> veJwJxrwList = veJwJxrwMapper.selectList(queryWrapper2);
        if(veJwJxrwList.size()<1){
            return "所选学期教学任务为空";
        }
        try {
            int xzbidTmp = 0;
            List<VeJwJxrw> jxrwPaiban = new ArrayList<VeJwJxrw>();
            for(int i=0;i<veJwJxrwList.size();i++){
                if(veJwJxrwList.get(i).getJxbid()!=xzbidTmp){ //第一排班或新排班
                    if(xzbidTmp != 0){  //属于新排班
                        createPaike(jxrwPaiban); //上一排班
                        jxrwPaiban.clear();
                    }
                    xzbidTmp = veJwJxrwList.get(i).getJxbid();
                }
                jxrwPaiban.add(veJwJxrwList.get(i));
                if(i==veJwJxrwList.size()-1){ //最后一条记录
                    createPaike(jxrwPaiban);
                }
            }
            if(pkmxCount==0){
                return "信息不完整，未生成排课明细";
            }
            return "OK:排课成功，共生成排课明细" + pkmxCount + "条，教学班：" + jxbmcShow;
        }
        catch (Exception ex){
            return ex.getMessage();
        }
    }

    /**
     * 生成排课信息保存
     * @param jxrwPaiban 属于一个教学班的教学任务列表（semid,jxbid）
     */
    private void createPaike(List<VeJwJxrw> jxrwPaiban) throws Exception{
        //新增排课信息
        VeJwJxrw veJwJxrw0 = jxrwPaiban.get(0);
        VeJwPaike veJwPaike = new VeJwPaike();
        veJwPaike.setSemid(veJwJxrw0.getSemid());
        veJwPaike.setJxbid(veJwJxrw0.getJxbid());
        QueryWrapper<VeJwPaike> queryWrapper = QueryGenerator.initQueryWrapper(veJwPaike,null);
        List<VeJwPaike> veJwJxPaikeList = veJwPaikeMapper.selectList(queryWrapper);
        if(veJwJxPaikeList.size()>0){
            veJwPaike = veJwJxPaikeList.get(0);
        }
        else  {
            BeanUtils.copyProperties(scheduleInfo, veJwPaike);
            veJwPaike.setJxbid(veJwJxrw0.getJxbid());
            String jxbmc = veJwJxrw0.getName().split("-")[0];
            veJwPaike.setJxbmc(jxbmc);
            veJwPaike.setQsz(0);
            veJwPaike.setZzz(0);
            veJwPaike.setKksmtid(0);
            veJwPaike.setTerminalid(1);
            veJwPaikeMapper.insert(veJwPaike);
        }
        if(veJwPaike.getId()==null || veJwPaike.getId()==0){
            throw new Exception("处理排课主表出错");
        }
        //新增排课信息 end
        if(true){ //scheduleInfo.getOverwrite()==1){
            veJwPkmxMapper.deleteByPkId(veJwPaike.getId().toString());
        }
        List<VeJwPkmxClass> veJwPkmxList = createPaikeMingxi(jxrwPaiban,veJwPaike);
        if(veJwJxPaikeList.size()>0){
            for(VeJwPkmxClass veJwPkmx:veJwPkmxList){
                veJwPkmxMapper.insert(veJwPkmx);
                pkmxCount ++;
            }
            jxbmcShow += (jxbmcShow.equals("")?"":"、")+ veJwPaike.getJxbmc();
        }
    }

    /**
     * 生成排课明细
     */
    private List<VeJwPkmxClass> createPaikeMingxi(List<VeJwJxrw> jxrwPaibanList, VeJwPaike veJwPaike)throws Exception{
        List<VeJwPkmxClass> veJwPkmxList = new ArrayList<VeJwPkmxClass>();//生成的排课明细
        int maxCa;//最大排课时间序号
        for(VeJwJxrw veJwJxrw:jxrwPaibanList){
            int zks = 0;// veJwJxrw.getWtime();//周课时，一周几节
            if(zks == 0) {
                zks = (veJwJxrw.getZxs() + veJwPkpz.getJkzq()-1)/ veJwPkpz.getJkzq();
            }
            for(int i=0;i<zks;i++){
                maxCa = caList.size()-1;
                if(maxCa<0){
                    break;
                }
                int zcs = ( zks + veJwPkpz.getLkjs()-1) / veJwPkpz.getLkjs();  //周次数
                int caNum = ( i / veJwPkpz.getLkjs())* maxCa / zcs;
                CourseArrange ca = caList.get(caNum);
                VeJwPkmxClass veJwPkmx =new VeJwPkmxClass();
                veJwPkmx.setPkid(veJwPaike.getId());
                veJwPkmx.setSemid(veJwJxrw.getSemid());
                veJwPkmx.setWeek(ca.getWeek());
                veJwPkmx.setSection(ca.getSection());
                veJwPkmx.setTeacherid(veJwJxrw.getTeacheruserid());
                veJwPkmx.setJsxm(veJwJxrw.getTeachername());
                veJwPkmx.setJsid(veJwJxrw.getJsid());
                veJwPkmx.setJsmc(jiaoShiMap.get(veJwJxrw.getJsid()));
                veJwPkmx.setKcid(veJwJxrw.getKcid());
                veJwPkmx.setKcmc(keChengMap.get(veJwJxrw.getKcid()));
                veJwPkmx.setTerminalid(1);

                veJwPkmx.setJxrwid(veJwJxrw.getId());
                veJwPkmx.setCreatetime(new Date());
                veJwPkmxList.add(veJwPkmx);
                caList.remove(caNum);
            }
        }
        return veJwPkmxList;
    }

    /**
     * 初始全部排课时间
     * @return
     */
    private List<CourseArrange> initCourseArrange(){
        List<CourseArrange> caList = new ArrayList<CourseArrange>();
        VeJwSection veJwSection = new VeJwSection();
        veJwSection.setStatus(1);
        QueryWrapper<VeJwSection> queryWrapper = QueryGenerator.initQueryWrapper(veJwSection,null);
        List<VeJwSection> seciontList = veJwSectionMapper.selectList(queryWrapper);
        List<VeJwBpksj> bpksjList = veJwBpksjMapper.selectList(null);
        for (int week =1;week<=7; week++) {
            for (VeJwSection section : seciontList) {
                boolean bpk = false;
                for (int i = 0; i < bpksjList.size(); i++) {
                    if (week == bpksjList.get(i).getWeek() && section.getId()== bpksjList.get(i).getSection()) {
                        bpk =true;
                        bpksjList.remove(i);
                        break;
                    }
                }
                if(!bpk){
                    CourseArrange ca = new CourseArrange(week,section.getId());
                    caList.add(ca);
                }
            }
        }
        return caList;
    }


}
